<template>
    <div class="register">
        <form ref="registerForm" @submit.prevent="handleRegister" class="register-form">
            <h3 class="title">注册</h3>
            <div class="form-item">
                <input
                        v-model="registerForm.username"
                        type="text"
                        autocomplete="off"
                        placeholder="账号"
                        required
                />
            </div>
            <div class="form-item">
                <input
                        v-model="registerForm.password"
                        type="password"
                        autocomplete="off"
                        placeholder="密码"
                        @keyup.enter="handleRegister"
                        required
                />
            </div>
            <div class="form-item">
                <input
                        v-model="registerForm.confirmPassword"
                        type="password"
                        autocomplete="off"
                        placeholder="确认密码"
                        @keyup.enter="handleRegister"
                        required
                />
            </div>
            <div class="form-item" style="width: 100%;">
                <button :disabled="loading" type="submit" style="width: 100%;">
                    <span v-if="!loading">注 册</span>
                    <span v-else>注 册 中...</span>
                </button>
            </div>
        </form>
        <!-- 底部 -->
        <div class="register-footer">
        </div>
    </div>
</template>

<script>
export default {
    name: "Register",
    data() {
        return {
            registerForm: {
                username: "",
                password: "",
                confirmPassword: ""
            },
            loading: false
        };
    },
    methods: {
        validatePassword() {
            if (this.registerForm.password !== this.registerForm.confirmPassword) {
                return '两次输入的密码不一致';
            }
            return '';
        },
        handleRegister() {
            const passwordError = this.validatePassword();
            if (passwordError) {
                alert(passwordError);
                return;
            }
            this.loading = true;
            // 这里添加你的注册逻辑
            this.$store.dispatch("Register", this.registerForm).then(() => {
                this.$router.push({ path: "/login" }).catch(() => {});
            }).catch(() => {
                this.loading = false;
            });
        }
    }
};
</script>

<style scoped>
.register {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
}
.title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
}
.register-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
}
.form-item {
    margin-bottom: 20px;
}
.form-item input {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    border: 1px solid #dcdcdc;
    border-radius: 4px;
    font-size: 14px;
}
.register-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}
</style>
